#include "opencv2/opencv.hpp"

#include<string>
#include <vector>
#include <algorithm>
#include "FingerTracker.h"
using namespace cv;
using namespace std;
using namespace cvb;

int K = 10;
int canny_threshold = 16;
int chain_threshold = 50;
int min_distance=5;//distance threshold for clustering

//finger tracker
FingerTracker tracker;

/// \fn ParamControl(int, void*)
/// \brief used as a parameter of createTrackbar function
void ParamControl(int, void*)
{
	tracker.SetParam(K,canny_threshold,chain_threshold);
}

int main()
{
	VideoCapture capture(0);

	if(!capture.isOpened()){
		std::cout<<"cannot read video!\n";
		return -1;
	}

	Mat frame;
	namedWindow("rgb");
	namedWindow("bw");

	//set parameters
	ParamControl(0,0);
	tracker.set_minDist(min_distance);

	while(true)
	{
		if(!capture.read(frame)){
			break;
		}

		Mat result,resultColor;

		tracker.Step(frame, &result,&resultColor);

		//color output image
		imshow("rgb", resultColor);

		//"binary" output image (white hand & red fingertips & green strings)
		imshow("bw",result);
		imwrite("result.bmp",result);

		//create trackbars to control parameters
		createTrackbar( "K:", "bw", &K, 50,ParamControl);
		createTrackbar( "canny_threshold:", "bw", &canny_threshold, 100,ParamControl);
		createTrackbar( "chain_threshold:", "bw", &chain_threshold, 500,ParamControl);

		waitKey(100);
	}

	capture.release();

	waitKey(0);
	return 0;
}